#!/bin/sh

# Get stock price from Yahoo finance service
# Author : James Tan
# Copyright: "(C) 20012-2013 James Tan. GNU GPL 3."
# Version: v0.12 (beta)

# History
# v0.1  - 11/20/2012
#   basic functionalities
# v0.11 - 12/08/2012
#   add external data file support
# v0.12 - 01/14/2014
#   add daily low and high output
#   add extra query string

# Usage
# eqmon.sh [datafile] [symbol ...]

# Format of EQS
#+ <name>[:symbol][,target price]
EQS=(
"TSE:OSPTX.TO"
"MFC:MFC.TO,cost@11.72/11.81"
"MPW"
"OHI"
"NLY,cost@14.38"
"NGG"
"MCD,watch@80.0"
"VIG"
"O"
"TICC"
"QCOR"
"BDCL"
"CTL,bid@36.64"
"GHDX,cost@28.10"
"TZYM"
"HCA"
"CVS"
"BMY"
)

INDEX=(
"S&P500:^gspc"
"TSX:^GSPTSE"
)

FIN_URL="http://download.finance.yahoo.com/d/quotes.csv?"
FIN_SQUERY="s0l1g0h0j0k0p2"   # short query
FIN_EQUERY=""  # extra query
#FIN_FQUERY="s0l1d1t1c1ohgv"

if [ "$1" ]; then
	if [ -f "$1" ]; then
		. $(pwd)/$1
	else
		EQS=($*)
	fi
fi
FIN_QUERY="${FIN_SQUERY}${FIN_EQUERY}"

get_info()
{
  _eq=$1
  key=${eq%%[:,]*}
  value=${eq#*[:]}
  symbol=${value%%,*}	
  userdata=""
  [ "$value" = "$symbol" ] || userdata=${value#*[,]}
  #echo $key $value $symbol $userdata
  res=$(curl -s "${FIN_URL}s=${symbol}&f=${FIN_QUERY}" | tr -d '\r')	
  echo -e "$key:\t${res#*,} - $userdata"
}

date
echo -e "symbol\tprice,daylow,dayhigh,yearlow,yearhigh,change%$otherfactors - watch price"
echo "======================================"
for eq in "${INDEX[@]}"; do
  get_info $eq
done
for eq in "${EQS[@]}"; do
  get_info $eq
done

